<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Alarm
 * Desc  110报警控制器
 * Create on 2025/5/13 9:34
 * Create by wangyafang
 */

namespace app\api\controller\wddj;


use addons\wddj\library\AlibabaCloudSms;
use app\admin\model\wddj\AlarmConfig;
use app\api\model\wddj\User;
use app\api\model\wddj\user\Passenger;
use app\api\model\wddj\user\UserRealNameAuthentication;
use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;

class Alarm extends Api
{
    protected $noNeedLogin = ['function_description'];
    protected $noNeedRight = ['*'];

    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\Alarm();
    }

    /**
     * Desc 报警
     * Create on 2025/5/13 9:35
     * Create by wangyafang
     */
    public function add()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $param = $this->request->post();
        $result = $this->validate($param,'app\api\validate\wddj\Alarm');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $wechat_id = User::getWechatId($this->auth->id);
        $source = $param['source'];
        $help_location = $param['help_location'];

        $add_data = array(
            'source'=>$source,
            'wechat_id'=>$wechat_id,
            'help_location'=>$param['help_location'],
            'lat'=>$param['lat'],
            'lng'=>$param['lng'],
            'status'=>2,
        );

        if ($source == 2) {
            $driverObj = (new \app\api\model\wddj\driver\Driver())->where('wechat_id',$wechat_id)->find();
            if (!$driverObj) {
                $this->error('不是司机，没有权限');
            }
            $certifiedDrivingAgentObj = (new \app\api\model\wddj\driver\CertifiedDrivingAgent())
                ->where('wechat_id',$wechat_id)->find();
            if ($certifiedDrivingAgentObj && empty($certifiedDrivingAgentObj['district'])) {
                $this->error('请完善认证信息');
            }
        }

        Db::startTrans();
        try {
            $this->model->data($add_data);
            $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $userWechatObj = (new \app\api\model\wddj\UserWechat())->get($wechat_id);
        $configObj = (new \app\admin\model\wddj\Config())->where('id',1)->find();
        if (!empty($configObj['alibaba_cloud_sign_name']) && !empty($configObj['alibaba_cloud_access_key_id']) && !empty($configObj['alibaba_cloud_access_key_secret'])) {
            $userSendSmsRequestParam = [
                "phoneNumbers" => $userWechatObj['mobile'],
                "templateCode" => $configObj['user_alarm_notify'],
                "templateParam" => "{'addres':'$help_location'}"
            ];
            AlibabaCloudSms::main($userSendSmsRequestParam);
            $passengerObj = (new Passenger())->where('wechat_id',$wechat_id)->order('id desc')->find();
            if ($passengerObj) {
                $userRealNameAuthenticationObj = (new UserRealNameAuthentication())->where('wechat_id',$wechat_id)->find();
                if ($userRealNameAuthenticationObj) {
                    $name = $userRealNameAuthenticationObj->getData('name');
                } else {
                    $name = $userWechatObj['nickname'];
                }
                $phone = $userWechatObj['mobile'];
                $friendSendSmsRequestParam = [
                    "phoneNumbers" => $passengerObj['mobile'],
                    "templateCode" => $configObj['friend_alarm_notify'],
                    "templateParam" => "{'name':'$name','phone':'$phone','addres':'$help_location'}"
                ];
                AlibabaCloudSms::main($friendSendSmsRequestParam);
            }
        }


        $this->success('报警成功');
    }

    /**
     * Desc 功能说明
     * Create on 2025/5/13 15:15
     * Create by wangyafang
     */
    public function function_description()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $content = (new AlarmConfig())->where('id',1)->value('function_description');
        $this->success('请求成功',array('content'=>$content));
    }
}



 